﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SGH
{
    class Correct
    {
        //先求得排样P使用的频率，再根据频率更新毛坯的剩余需求，根据剩余的需求修改毛坯价值
        public static int updateBlankInfo(int[,] p, Global global)
        {
            int n = p.GetLength(0);
            // int m = Gen.restDemand.Count;
            double[] blankSquer = new double[n];
            double totalSquer = 0.0;
            int minFrequency = 100000;
            for (int i = 0; i < n; i++)
            {
                if (p[i, 1] != 0)
                {
                    double r = Constant.diameter[p[i, 0]] / 2;
                    double squer = Constant.PI * r * r;
                    blankSquer[i] = squer;
                    totalSquer += squer * p[i, 1];
                    for (int j = 0; j < global.restDemand.Count; j++)
                    {
                        if (global.restDemand[j][0] == p[i, 0])
                        {
                            double num = p[i, 1] + 0.0;
                            int frequency = (int)(global.restDemand[j][1] / num);
                            if (frequency < minFrequency) minFrequency = frequency;
                        }
                    }
                }
            }
            if (minFrequency == 100000)
            {
                minFrequency = 0;
            }
            for (int s = 0; s < global.restDemand.Count; s++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (global.restDemand[s][0] == p[j, 0])
                    {
                        global.restDemand[s][1] -= (p[j, 1] * minFrequency);
                        if (global.restDemand[s][1] <= 0)
                        {
                            global.restDemand.RemoveAt(s);
                        }
                    }
                }
            }
            //修改毛坯价值
            double u = totalSquer / (Constant.sheetLength * Constant.sheetWidth);
            int restNum = global.restDemand.Count;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < restNum; j++)
                {
                    int id = p[i, 0];
                    if (global.restDemand[j][0] == id)
                    {
                        double g2 = Constant.emixilo * p[i, 1] / (Constant.demand[p[i, 0]] + global.restDemand[j][1]);
                        double g1 = 1 - g2;
                        global.blankValue[id] = g1 * global.blankValue[id] + g2 * Math.Pow(blankSquer[i], Constant.p) / u;
                    }
                }
            }
            return minFrequency;
        }
    }
}
